package org.app.original;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


import org.app.Ports;
import org.app.MyUtils;
import org.app.data.MyBean;
import org.app.entities.Info;
import org.app.entities.MyCoordinate;
import org.app.entities.Users;

public class MyXML {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		char c='z';
		System.out.println(new StringBuffer(c).append('j'));
		Class type=MyBean.class;
		System.out.println(type.getClassLoader()==null);
		
		MyXML demo=new MyXML();
		//MyCoordinate my=new MyCoordinate();
		Users my=new Users();
		
		List list=new ArrayList();
		list.add(new Users());
		list.add(new Users());
		my.setList(list);
		my.setInfo(new Info());
		Map map=demo.toMap(my);
		MyUtils.show(map);
		
		System.out.println(my.getClass().getClassLoader()==null);
		String temp=demo.toXML(my).toString();
		
		System.out.println(temp);
		System.out.println("^_^");
	}
	
	/**
	 * 加密json即转换为bean
	 * @param type
	 * @param json
	 * @return
	 */
	public <T>T Encrypt(Class<T> type,String json){
		return null;
	}
	
	/**
	 * 解密 bean转换为xml
	 * @param t
	 * @return
	 */
	public <T>String Decrypt(T t){
		return toXML(t).toString();//未格式化处理
	}
	
	private <T>Map toMap(final T t){
		final Map<String,Object> map=new HashMap();
	//	System.out.println(t);
		MyBean.visit(t, new Ports(){

			@Override
			public Object execute(Object obj) {
				// TODO Auto-generated method stub
			//	if(obj instanceof PropertyDescriptor){
					PropertyDescriptor pd=(PropertyDescriptor)obj;
				//	System.out.println(pd.getName());
					try {
						Class type=pd.getPropertyType();
						String name=pd.getName();
						Object value=pd.getReadMethod().invoke(t);
					
					
						map.put(pd.getName(), value);
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			//	}
				return null;
			}
			
		});
		return map;
	}
	
	public <T>StringBuffer toXML(T t){
		StringBuffer sb=new StringBuffer();
		if(t==null)return sb;
		if(t instanceof Number){
			sb.append(t);
		}else if(t instanceof String){
			sb.append(t);
		}else if(t.getClass().getClassLoader()!=null){//用户自定义类型
			Map map=this.toMap(t);
			sb.append(toXML(map));
		}else if(t instanceof Map){
			//sb.append(t);
			
			Map<String,Object> map=(Map)t;
			Object obj=map.get("class");
			Class type=(Class) map.get("class");
			String name=type.getSimpleName();
			sb.append(String.format("<%s type='%s'>", name,type));
			Set<Entry<String, Object>> set=map.entrySet();
			for(Entry<String,Object> entry:set){
				 name=entry.getKey();
				if(name.equals("class")){
					continue;
				}
				StringBuffer value=toXML(entry.getValue());
				
//				if(value.length()==0){
//					continue;
//				}
				
				sb.append(MessageFormat.format("<{0}>{1}</{0}>", name,value));
				
			}
			
			
			//sb=new StringBuffer(MessageFormat.format("<{0} type='{1}'>{2}</{0}>", name,type,sb.toString()));
			//sb.insert(0, String.format("<%s type='%s'>", name,type.toString())).append("</").append(name).append(">");
		
			sb.append(String.format("</%s>", type.getSimpleName()));
		
		}if(t instanceof List){
			//sb.append(t);
			
			List list=(List)t;
			int size=list.size();
			sb.append("<List>");
			for(int i=0;i<size;i++){
			//	sb.append(String.format("<value id='%d'>%s</value>", i,toXML(list.get(i))));
				sb.append(String.format("<value >%s</value>", toXML(list.get(i))));
			}
			sb.append("</List>");
		}if(t.getClass().isArray()){
			sb.append("<Array>");
			int size=Array.getLength(t);
			for(int i=0;i<size;i++){
			
				sb.append(String.format("<value id='%d'>%s</value>", toXML(Array.get(t, i)),i));
				}
			
			sb.append("</Array>");
	
		}
		return sb;
	}
	
	

}
